/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.util;

import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.render.BlendMode;

/**
 * 设置图片颜色
 *
 * @since 2021-04-14
 */
public class ImageTint {
    private static final int INT_TWO = 2;

    private ImageTint() {
    }

    /**
     * 设置Element着色
     *
     * @param element 目标对象
     * @param intColor 着色颜色
     * @return 设置后的对象
     */
    public static Element getTintElement(Element element, int intColor) {
        return getTintElement(element, intColor, BlendMode.SRC_ATOP);
    }

    /**
     * 设置Element着色
     *
     * @param element 目标对象
     * @param intColor 着色颜色
     * @param mode BlendMode
     * @return 设置后的对象
     */
    public static Element getTintElement(Element element, int intColor, BlendMode mode) {
        int[] colors = new int[]{intColor, intColor};
        int[][] states = new int[INT_TWO][];
        states[0] = new int[]{0};
        states[1] = new int[]{0};
        element.setStateColorList(states, colors);
        element.setStateColorMode(mode);
        return element;
    }

    /**
     * 设置Element着色
     *
     * @param view 要设置element的组件对象
     * @param element 目标对象
     * @param intColor 着色颜色
     * @param mode BlendMode
     */
    public static void setImageTint(Image view, Element element, int intColor, BlendMode mode) {
        view.setImageElement(getTintElement(element, intColor, mode));
    }
}
